<?php

class LingkunganController extends BaseController {

	public function index() {                
        $provinsis  = Provinsi::active()->get();
        $kabupatens = Kabupaten::active()->get();                        
        $kecamatans = Kecamatan::active()->get();            
        $kelurahans = Kelurahan::active()->get();

        if(Lingkungan::active()->count() == 0) {

            return View::make('lingkungan.index')                
                ->with(compact('kelurahans'))
                ->with(compact('kecamatans'))
                ->with(compact('kabupatens'))
                ->with(compact('provinsis'))
                ->with('message', 'Harap dilengkapi.');

        }else{
            $lingkungan = Lingkungan::active()->first()->toArray();            
        
            return View::make('lingkungan.index', $lingkungan)
                ->with(compact('kelurahans'))
                ->with(compact('kecamatans'))
                ->with(compact('kabupatens'))
                ->with(compact('provinsis'));
        }
	}    

    public function uploadLogo() {
        $file = Input::file('logo');

        $destinationPath = Lingkungan::$logoPath;
        $filename        = $file->getClientOriginalName();
        $img = Image::make($file->getRealPath())->resize(150, 150)->save(public_path().$destinationPath.$filename);

        if($img) {
            return Response::json(['type' => 'success', 'full_filename' => asset($destinationPath.$filename), 'filename' => $filename]);
        }else{
            return Response::json(['type' => 'error', 'msg' => 'Upload gambar sukses']);
        }
    }

    public function uploadTTDKepling() {
        $file = Input::file('ttd_kepling');

        $destinationPath = Lingkungan::$ttdPath;
        $filename = $file->getClientOriginalName();
        $img = Image::make($file->getRealPath())->resize(150, 80)->save(public_path().$destinationPath.$filename);

        if($img) {
            return Response::json(['type' => 'success', 'full_filename' => asset($destinationPath.$filename), 'filename' => $filename]);
        }else{
            return Response::json(['type' => 'error', 'msg' => 'Upload gambar sukses']);
        }   
    }

    public function uploadTTDKeldat() {
        $file = Input::file('ttd_keldat');

        $destinationPath = Lingkungan::$ttdPath;
        $filename = $file->getClientOriginalName();
        $img = Image::make($file->getRealPath())->resize(150, 80)->save(public_path().$destinationPath.$filename);

        if($img) {
            return Response::json(['type' => 'success', 'full_filename' => asset($destinationPath.$filename), 'filename' => $filename]);
        }else{
            return Response::json(['type' => 'error', 'msg' => 'Upload gambar sukses']);
        }   
    }

    public function update() {
        if(Lingkungan::active()->count() == 0) {
            // insert

            $validator = Validator::make($data = Input::all(), Lingkungan::$rules);

            if ($validator->fails())
            {
                $messages = $validator->messages()->all();
                return Response::json(['type' => 'error', 'msg' => $messages]);
            }else{                

                $lingkungan = Lingkungan::create($data);
                $lingkungan->ref_id = $lingkungan->id;
                $lingkungan->save();

                return Response::json(['type' => 'success', 'msg' => 'Update sukses']);
            }
        }else{
            // update                
            $validator = Validator::make($data = Input::all(), Lingkungan::$rules);

            if ($validator->fails())
            {
                $messages = $validator->messages()->all();
                return Response::json(['type' => 'error', 'msg' => $messages]);
            }else{

                $lingkungan            = Lingkungan::active()->first();                
                $data['ref_id']        = $lingkungan->ref_id;
                $data['action']        = 2;

                $lingkungan->is_active = 0;
                $lingkungan->save();

                Lingkungan::create($data);
                return Response::json(['type' => 'success', 'msg' => 'Update sukses']);
            }
        }
    }

    public function getPolygonJSON()
    {
        $lingkungan = Lingkungan::active()->first();
        return Response::json($lingkungan->polygon);        
    }

}